package TcpUdpDemp;

import java.io.*;
import java.net.*;

public class bb {
    public static void main(String[] args) {
        String hostname = "localhost"; // 服务端主机名
        int port = 1234; // 服务端端口
        try (Socket socket = new Socket(hostname, port);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {

            out.println("hello"); // 客户端发送消息到服务端

            String fromServer = in.readLine(); // 读取服务端的回复
            System.out.println("Server says: " + fromServer);

            String fromUser = stdIn.readLine(); // 客户端用户输入
            if ("bye".equalsIgnoreCase(fromUser)) {
                System.out.println("Client is shutting down");
                socket.close();
            }
        } catch (UnknownHostException ex) {
            System.out.println("Server not found: " + ex.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}